﻿using MQTTnet.Server;
using MQTTnet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MQTTnet.Protocol;
using System.Reflection.Metadata;
using UtilsMQ.Model;
using System.Diagnostics;
using Newtonsoft.Json;
using MQTTnet.Extensions.WebSocket4Net;
using MQTTnet.Client;
using WebSocket4Net;
namespace UtilsMQ
{
    public class UtilsMqttServerWebSocket
    {


        public static async Task MQTTP()
        {
            var factory = new MqttFactory();
            var client = factory.CreateMqttClient();

            var options = new MqttClientOptionsBuilder()
                .WithTcpServer("0.0.0.0", 1883)
                .Build();

            await client.ConnectAsync(options);
            while (true)
            {
                Console.WriteLine("输入要发布的信息: ");
                var message = Console.ReadLine();

                var mqttMessage = new MqttApplicationMessageBuilder()
                    .WithTopic("testTopic")
                    .WithPayload(Encoding.UTF8.GetBytes(message))
                    .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
                    .Build();

                await client.PublishAsync(mqttMessage);
            }
        }

    }
}
